{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "569d620b-882a-4d90-a82a-e0c2bb85de9d",
   "metadata": {},
   "source": [
    "\n",
    "姚端正、周国全、贾俊基《数学物理方法》第四版P.275\n",
    "\n",
    "伽辽金法重解例题3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e9220533-845d-4da3-88cf-4e204859b2e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *  # 导入sympy 包中所有的函数\n",
    "from sympy.abc import x,y # 引入默认的符号变量\n",
    "c0,c1,lamd = symbols('c0 c1 lambda') # 自定义符号变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b1db7b4c-a630-474c-8c8a-0e2f2e33cc43",
   "metadata": {},
   "outputs": [],
   "source": [
    "ux = x*(x-1)*(c0+c1*x)\n",
    "v1 = x*(x-1)\n",
    "v2 = x**2 *(x-1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "81a5c4fa-ff42-41c2-ba96-ca44ec677482",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{c_{0}}{3} + \\frac{c_{1}}{6} - \\lambda \\left(\\frac{c_{0}}{30} + \\frac{c_{1}}{60}\\right)$"
      ],
      "text/plain": [
       "c0/3 + c1/6 - lambda*(c0/30 + c1/60)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "J_1  = integrate(diff(ux,x)*diff(v1,x),(x, 0, 1)) - lamd* integrate(ux*v1,(x, 0, 1))\n",
    "J_1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "edc2a25b-7805-4bd7-9f56-3c99c6fa5bdf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{c_{0}}{6} + \\frac{2 c_{1}}{15} - \\lambda \\left(\\frac{c_{0}}{60} + \\frac{c_{1}}{105}\\right)$"
      ],
      "text/plain": [
       "c0/6 + 2*c1/15 - lambda*(c0/60 + c1/105)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "J_2  =  integrate(diff(ux,x)*diff(v2,x),(x, 0, 1)) - lamd* integrate(ux*v2,(x, 0, 1))\n",
    "J_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "25edbd4e-f5cc-4198-9adc-727b92971e53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle c_{0} \\cdot \\left(\\frac{1}{3} - \\frac{\\lambda}{30}\\right) + c_{1} \\cdot \\left(\\frac{1}{6} - \\frac{\\lambda}{60}\\right)$"
      ],
      "text/plain": [
       "c0*(1/3 - lambda/30) + c1*(1/6 - lambda/60)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collect(expand(J_1), (c0,c1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "69f46e53-7489-42ea-988c-46b5bed6d127",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle c_{0} \\cdot \\left(\\frac{1}{6} - \\frac{\\lambda}{60}\\right) + c_{1} \\cdot \\left(\\frac{2}{15} - \\frac{\\lambda}{105}\\right)$"
      ],
      "text/plain": [
       "c0*(1/6 - lambda/60) + c1*(2/15 - lambda/105)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collect(expand(J_2), (c0,c1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "19071805-db55-4ac0-b6d1-99449c30a5e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 0\\\\0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[0, 0],\n",
       "[0, 0]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_c=zeros(2)\n",
    "matrix_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "de5a1f7c-c568-49d8-9a61-42411ed5c9dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = [c0,c1]\n",
    "J = [J_1,J_2]\n",
    "for i in [0,1]:\n",
    "    for j in [0,1]:\n",
    "        matrix_c[i,j]= expand(J[i]).coeff(c[j])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "26353137-749f-4f00-8fbc-7df8633c1c72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{1}{3} - \\frac{\\lambda}{30} & \\frac{1}{6} - \\frac{\\lambda}{60}\\\\\\frac{1}{6} - \\frac{\\lambda}{60} & \\frac{2}{15} - \\frac{\\lambda}{105}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1/3 - lambda/30,   1/6 - lambda/60],\n",
       "[1/6 - lambda/60, 2/15 - lambda/105]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "41ea76a9-df1f-4662-b538-903745ae24d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{\\lambda^{2}}{25200} - \\frac{13 \\lambda}{6300} + \\frac{1}{60}$"
      ],
      "text/plain": [
       "lambda**2/25200 - 13*lambda/6300 + 1/60"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_c.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "4b7aea0c-06b2-4aec-a70b-83b5fa415006",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 42]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sol_lamd = solve(matrix_c.det(),lamd)\n",
    "sol_lamd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "f0993d26-6da9-4ea7-a2cb-7ab9905f170f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 10$"
      ],
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sol_lamd[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "2805e586-4b89-4da4-8641-9284c575db05",
   "metadata": {},
   "outputs": [],
   "source": [
    "condition = integrate(ux*ux,(x, 0, 1)) -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "f5f452d0-820a-46ce-927b-22cb3c1e44eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(-sqrt(30), 0), (sqrt(30), 0)]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求 c0,c1 \n",
    "sol_1 = solve([J_1.subs(lamd,sol_lamd[0]), J_2.subs(lamd,sol_lamd[0]),condition], c0,c1)\n",
    "sol_1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "217615b0-b8c5-4d71-8649-cf10c54fa08a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(-sqrt(210), 2*sqrt(210)), (sqrt(210), -2*sqrt(210))]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求 c0,c1 \n",
    "sol_2 = solve([J_1.subs(lamd,sol_lamd[1]), J_2.subs(lamd,sol_lamd[1]),condition], c0,c1)\n",
    "sol_2 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ef2fbce-3548-4085-897c-716e5217dd7b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
